//
//  EventInfoViewController.m
//  KG
//
//  Created by Eugene Braginets on 11.04.12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "EventInfoViewController.h"
#import "EventItem.h"
#import "AppDelegate.h"
#import "bannerView.h"


@implementation EventInfoViewController
@synthesize bannerViewController=_bannerViewController;
@synthesize webView = _webView;
@synthesize eventID=_eventID;
@synthesize activityIndicator = _activityIndicator;
@synthesize event=_event;
- (id)initWithEvent: (EventItem *)event {
    self = [super initWithNibName:@"EventInfoViewController" bundle:nil];
    if (self) {
        // Custom initialization
        NSLog(@"Event: %@",event.itemid);
        self.eventID=event.itemid;
        self.event=event;
        NSString* url=[NSString stringWithFormat:@"%@&item=%@",kGetEventURL,self.eventID];
        NSString* name=[NSString stringWithFormat:@"%@%@",kEventItem,self.eventID];
        [myAppDelegate getData:url andType:name];
        self.navigationItem.title=event.startdate;
        
        //        NSString* name=[NSString stringWithFormat:@"%@%@",kPitchItem,self.placeID];
        
        //    NSLog(@"name: %@",[self.playerItem name]);
        
        
        [[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector: @selector(reload:)
                                                     name:name 
                                                   object: nil];
     }
    return self;
}



#pragma mark - View lifecycle

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
	// the user clicked one of the OK/Cancel buttons
	if (buttonIndex == [actionSheet destructiveButtonIndex])
	{
		//----- CLICKED OK -----
        EKEventStore *eventStore = [[EKEventStore alloc] init];
        
        EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
        event.title     = self.event.name;
        
        event.startDate = [[NSDate alloc] initWithTimeIntervalSince1970:[self.event.startdatetimestamp intValue]];
        event.endDate   = [[NSDate alloc] initWithTimeInterval:600 sinceDate:event.startDate];
        event.location=self.event.venue;
        event.notes=@"Added by KerryGaa iPhone Application";
        
        [event setCalendar:[eventStore defaultCalendarForNewEvents]];
        NSError *err;
        [eventStore saveEvent:event span:EKSpanThisEvent error:&err];   
        NSLog (@"event is added!");
   }
	else if (buttonIndex == [actionSheet cancelButtonIndex])
	{
		NSLog(@"CANCEL!");
        //----- CLICKED CANCEL -----
        
	}
}
- (void) addToiCal {

    
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Add this event to your Calendar?"
                             delegate:self
                    cancelButtonTitle:@"Cancel"
               destructiveButtonTitle:@"OK"
                    otherButtonTitles:nil];
	[actionSheet showInView:self.view];
    
    
//    EventToiCalViewController* viewController=[[EventToiCalViewController alloc] init ];
//    [self.navigationController pushViewController:viewController animated:YES];
}

- (void)viewDidLoad
{
   
    UIBarButtonItem *logoutButton = [[UIBarButtonItem alloc] 
                                     initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addToiCal)] ;
    self.navigationItem.rightBarButtonItem = logoutButton;
    [self.view setBackgroundColor:[UIColor colorWithRed:.3 green:.6 blue:.1 alpha:1]];

    
    
    [self.webView setHidden:YES];
    [self.webView setBackgroundColor:[UIColor clearColor]];
    [self.webView setOpaque:NO];
    
    [self.activityIndicator setHidden:NO];
    [self.activityIndicator startAnimating];
    


    [self.webView setOpaque:NO];
//    self.event= [[[myAppDelegate dataArraysStore] objectForKey:[NSString stringWithFormat:@"%@%@",kEventItem,self.eventID]] objectAtIndex:0];
//    
    NSString* str;
    //    NSLog(@"imageAddr: %@",self.place.imageAddr);
    //    NSLog(@"Descr: %@",self.place.description);
//    if (!self.place.description){
//        self.place.description=@"";
//    }
    
    str=[NSString stringWithFormat:@"<IMG title=\"loading...\" src=\"%@\" width=\"305px\"><h2>%@</h2><h3>%@</h3> %@ <br/>",self.event.imageAddr,self.event.name,self.event.venue,self.event.description];
    [self.webView loadHTMLString:str baseURL:nil];

    bannerView *bannerVC=[[bannerView alloc] init];
    [self.view addSubview:bannerVC.view];
    [bannerVC animate];
    self.bannerViewController=bannerVC;

    
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [self setWebView:nil];
    [self setActivityIndicator:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillDisappear:(BOOL)animated
{
    
//    [[NSNotificationCenter defaultCenter] 
//     removeObserver:self
//     name:[NSString stringWithFormat:@"%@%@",kPitchItem,self.placeID]
//     object:nil];
    [self.webView stopLoading];
    self.webView.delegate = nil;
    
    [super viewWillDisappear:animated];
    
}

#pragma mark - WebView methods

- (void)webViewDidStartLoad:(UIWebView *)webView {
    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    
    //  self.careerWebView.frame = CGRectMake(320, 10, 315, 350);
    self.webView.hidden = NO;
    self.webView.alpha=0;
    [UIView animateWithDuration:0.4 animations:^{
        //        self.careerWebView.frame = CGRectMake(0, 10, 315, 350);
        self.webView.alpha=1;
    }];
    
    [self.activityIndicator setHidden:YES]; 
    
    
}


#pragma mark - custom Methods
- (void) reload:(NSNotification*) notify {
    NSLog (@"reload %@%@",kEventItem,self.eventID);
    
    
    
}



#pragma mark - other ViewController delegate Methods
- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
